package com.jiezuo.common.config.mybatisplus;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.jiezuo.auth.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @description 处理新增和更新的基础数据填充，配合BaseEntity和MyBatisPlusConfig使用
 * @author 庞留杰
 * @date 2020/7/22 20:38
 * @version V1.0
 **/
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    /**
     * 功能描述: 新增数据执行
     * @author: 庞留杰
     * @param metaObject
     * @return:
     * @exception
     * @date: 2020/7/16 19:20
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {//定时任务会进入此处
            this.setFieldValByName("created_time", new Date(), metaObject);
            this.setFieldValByName("updated_time", new Date(), metaObject);
        } else {
            HttpServletRequest request = requestAttributes.getRequest();
            Object obj = request.getAttribute("user");
            User user = JSON.parseObject(obj + "", User.class);
            String user_id = null;
            if (user != null) {
                user_id = user.getId();
            }
            this.setFieldValByName("created_time", new Date(), metaObject);
            this.setFieldValByName("created_by", user_id, metaObject);
            this.setFieldValByName("updated_time", new Date(), metaObject);
            this.setFieldValByName("updated_by", user_id, metaObject);
//            //创建企业
//            this.setFieldValByName("created_enterprise", user.getEnte_ancestors(), metaObject);
//            //创建部门
//            this.setFieldValByName("created_dept", user.getDept_ancestors(), metaObject);
        }
    }

    /**
     * 功能描述: 更新数据执行
     * @author: 庞留杰
     * @param metaObject
     * @return:
     * @exception
     * @date: 2020/7/16 19:20
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {//定时任务会进入此处
            this.setFieldValByName("updated_time", new Date(), metaObject);
        } else {
            HttpServletRequest request = requestAttributes.getRequest();
            Object obj = request.getAttribute("user");
            User user = JSON.parseObject(obj + "", User.class);
            String user_id = null;
            if (user != null) {
                user_id = user.getId();
            }
            this.setFieldValByName("updated_by", user_id, metaObject);
            this.setFieldValByName("updated_time", new Date(), metaObject);
//            //创建企业
//            this.setFieldValByName("created_enterprise", user.getEnte_ancestors(), metaObject);
//            //创建部门
//            this.setFieldValByName("created_dept", user.getDept_ancestors(), metaObject);
        }
    }

}
